package com.hopu.oa.dao.mail;

import com.hopu.oa.entity.mail.Inmaillist;
import com.hopu.oa.entity.mail.Mailreciver;
import com.hopu.oa.entity.mail.Pagemail;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.plan.AttachmentList;
import com.hopu.oa.entity.process.AoaAttachmentList;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface mailDao {

    //未读邮件查询
    List<Mailreciver> findByReadAndDelAndReciverId(Integer isRead, Integer isDel, Integer mailReciverId);

    //查找创建了但是却没有发送的邮件
    @Select("select * from aoa_in_mail_list where mail_push = #{bo} and mail_del =#{isDel} and mail_in_push_user_id = #{userId}")
    List<Inmaillist> findByPushAndDelAndMailUserid(Integer bo, Integer isDel, Integer userId);

    //查找删除的邮件条数
    List<Mailreciver> findByDelAndReciverId(Integer b, Integer userId, String userName);

    //收件箱
    List<Pagemail> recive(@Param("find") String find, @Param("page") Integer page, @Param("size") Integer size, @Param("userId") Integer userId);

    //发件箱
    List<Pagemail> send(@Param("find") String find, @Param("page") Integer page, @Param("size") Integer size, @Param("userId") Integer userId);

    //草稿箱
    List<Pagemail> draft(@Param("find") String find, @Param("page") Integer page, @Param("size") Integer size, @Param("userId") Integer userId);

    //垃圾箱
    List<Pagemail> trashCan(@Param("find") String find, @Param("page") Integer page, @Param("size") Integer size, @Param("userId") Integer userId, String userName);

    @Select("select * from aoa_mail_reciver where mail_reciver_id = #{userId} and mail_id = #{id}")
    Mailreciver findbyReciverIdAndmailId(Integer userId, Integer id);

    @Update("update aoa_in_mail_list set mail_del = #{isDel} where mail_id = #{mailId}")
    int setDel(Integer isDel, Integer mailId);

    @Update("update aoa_mail_reciver set is_del = #{isDel} where mail_id = #{mailId}")
    int saveDel(Integer isDel, Integer mailId);

    @Update("update aoa_mail_reciver set is_del = #{isDel} where mail_id = #{mailId} and mail_reciver_id = #{userId}")
    int saveShouDel(Integer isDel, Integer mailId, Integer userId);

    Inmaillist selectByMailId(Integer mailId);

    @Update("update aoa_in_mail_list set mail_del = #{isDel} where mail_id = #{mailId}")
    int setRead(Integer isDel, Integer mailId);

    @Update("update aoa_mail_reciver set is_read = #{isRead} where mail_id = #{mailId}")
    int saveRead(Integer isRead, Integer mailId);

    @Select("select * from aoa_attachment_list where attachment_id = #{attachmentId}")
    AttachmentList selectAttachmentListByFileId(Integer attachmentId);

    Pagemail WriteAttachmentListByFileId(Integer Id);

    @Select("select * from aoa_type_list where type_id <=18 and type_id >=16")
    List<AoaTypeList> selectTypeList();

    @Select("select * from aoa_status_list where status_id>=20 and status_id<=22")
    List<AoaStatusList> selectStatusList();

    int sendEmail(Inmaillist inmaillist);

    int sendReciver(Mailreciver mailreciver);

    //查询是否已读
    @Select("select * from aoa_mail_reciver where mail_id =#{mailId} and mail_reciver_id =#{userId}")
    Mailreciver selectByMailreciverbyMailId(Integer mailId, Integer userId);

    //已读邮件
    @Update("update aoa_mail_reciver set is_read = 1 where mail_id = #{mailId}")
    int updateEmail(Integer mailId);

    //通过id查询附件
    @Select("select * from aoa_attachment_list where attachment_id = #{attachmentId}")
    AoaAttachmentList selectAoaAttachmentListById(Integer attachmentId);

    @Delete("delete from aoa_in_mail_list where mail_id = #{mailId}")
    int deletEmail(Integer mailId);

    @Delete("delete from aoa_mail_reciver where mail_id = #{mailId} and mail_reciver_id = #{userId}")
    int deletMailReciverShou(Integer mailId, Integer userId);

    @Delete("delete from aoa_mail_reciver where mail_id = #{mailId} and mail_reciver_id = #{userId}")
    int deletMailReciverSend(Integer mailId, Integer userId);

    @Update("update aoa_in_mail_list set mail_star = #{star} where mail_id = #{mailId}")
    int statEmail(Integer star, Integer mailId);

    @Update("update aoa_mail_reciver set is_read = #{isRead} where mail_reciver_id = #{userId} and mail_id = #{mailId}")
    int isRead(Integer userId, Integer mailId, Integer isRead);

}
